﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;
using FbKPI.Facade.Validator;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
using System.Web.Script.Services;
using System.Transactions;

namespace FBKPI.ProcessRequest
{
    /// <summary>
    /// Summary description for KPIBreakDownHandler
    /// </summary>
    public class KPIBreakDownHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string msg = this.SaveInputKPIBreakDown();
            context.Response.Write(msg);
        }

        private string SaveInputKPIBreakDown()
        {
            string msg = "OK";
            sysLogFacade sysLogFacade = new sysLogFacade();
            KPIFacade KpiFacade = new KPIFacade();
            sysLog objSysLog = new sysLog();
            Employee CurrUser = new Employee();
            try
            {

                //string action = HttpContext.Current.Request.QueryString["action"];
                int KpiID = Convert.ToInt16(HttpContext.Current.Request.QueryString["KpiID"]);
                int groupID = Convert.ToInt16(HttpContext.Current.Request.QueryString["groupID"]);
                int categoryID = Convert.ToInt16(HttpContext.Current.Request.QueryString["categoryID"]);
                int typeID = Convert.ToInt16(HttpContext.Current.Request.QueryString["typeID"]);
                int subtypeID = Convert.ToInt16(HttpContext.Current.Request.QueryString["subtypeID"]);
                int uomID = Convert.ToInt16(HttpContext.Current.Request.QueryString["uomID"]);
                int uomMaxID = Convert.ToInt16(HttpContext.Current.Request.QueryString["uomMaxID"]);
                int uomMinID = Convert.ToInt16(HttpContext.Current.Request.QueryString["uomMinID"]);
                int year = Convert.ToInt16(HttpContext.Current.Request.QueryString["year"]);
                int weight = Convert.ToInt16(HttpContext.Current.Request.QueryString["weight"]);
                string periode = HttpContext.Current.Request.QueryString["periode"];
                string polarization = HttpContext.Current.Request.QueryString["polarization"];
                string description = HttpContext.Current.Request.QueryString["description"];
                int IsActive = Convert.ToInt16(HttpContext.Current.Request.QueryString["IsActive"]);
                int IsSummaryValue = Convert.ToInt16(HttpContext.Current.Request.QueryString["IsSummaryValue"]);
                CurrUser = new EmployeeFacade().GetEmployeeByID(Convert.ToInt32(HttpContext.Current.Request.QueryString["CurrUser"]));

                KPI objKpi = new KPI();
                KPIAssign objKpiAssign = new KPIAssign();
                KPIAssignDetail objKpiAssignDetail = new KPIAssignDetail();

                KPI getKPI = KpiFacade.GetKPIByID(KpiID);
                if (getKPI != null)
                {
                    objKpi = new KPI();
                    objKpi.ID=0;
                    objKpi.Description = description;
                   

                    objKpi.WeightEnableFlag = getKPI.WeightEnableFlag;
                    objKpi.UoMEnableFlag =getKPI.UoMEnableFlag;
                    objKpi.GroupKPI = new GroupKPIFacade().GetGroupKPIByID(groupID);
                    objKpi.CategoryKPI = new CategoryKPIFacade().GetCategoryKPIByID(categoryID);
                    objKpi.TypeKPI = new TypeKPIFacade().GetTypeKPIByID(typeID);
                    objKpi.SubTypeKPI = new SubTypeKPIFacade().GetSubTypeKPIByID(subtypeID);
                    objKpi.UoM = new UoMFacade().GetUoMByID(uomID);
                    objKpi.UoMDetailMax = new UoMDetailFacade().GetUoMDetailByID(uomMaxID);
                    objKpi.UoMDetailMin = new UoMDetailFacade().GetUoMDetailByID(uomMinID);
                    objKpi.KPIParent = getKPI;// KpiID;
                    //KPIs.KPIReplacement = new KPIFacade().GetKPIByID(KPIReplacementID);
                    objKpi.Year = year;
                    objKpi.Weight = weight;
                    objKpi.Periode = periode;
                    objKpi.Polarization = polarization;
                    objKpi.TargetDescription = getKPI.TargetDescription;
                    objKpi.ManualCalculation = getKPI.ManualCalculation;
                    objKpi.KPICalculationType = getKPI.KPICalculationType;
                    objKpi.ReplacementDate = getKPI.ReplacementDate;
                    objKpi.ReplacementDescription = getKPI.ReplacementDescription;
                    objKpi.KPIDetailInformation = getKPI.KPIDetailInformation ;
                    objKpi.KPIStrategicObjective = getKPI.KPIStrategicObjective;
                    objKpi.CreateBy = CurrUser.Email;
                    objKpi.BusinessGroupID = CurrUser.BusinessGroupID;
                    objKpi.EmployeeIDVerifier = CurrUser.ID;
                    objKpi.isLockWeight = true;
                    objKpi.PercentageMax = 100;
                    objKpi.PercentageMin = 80;
                    objKpi.AssignWeight = "1";
                    objKpi.isActive = IsActive == 0 ? false : true;
                    objKpi.isSummaryValue = IsSummaryValue == 0 ? false : true;

                    objKpiAssign = new KPIAssign();
                    objKpiAssign.ID=0;
                    objKpiAssign.isActive=true;
                    objKpiAssign.KPI.ID = KpiID;
                    objKpiAssign.Location = CurrUser.SubBranch.Location;
                    objKpiAssign.ManualCalculation = getKPI.ManualCalculation;
                    objKpiAssign.Polarization =getKPI.Polarization;
                    objKpiAssign.RGM =CurrUser.SubBranch.Location.RGM;
                    objKpiAssign.SubBranch =CurrUser.SubBranch;
                    objKpi.UoM = new UoMFacade().GetUoMByID(uomID);
                    objKpi.UoMDetailMax = new UoMDetailFacade().GetUoMDetailByID(uomMaxID);
                    objKpi.UoMDetailMin = new UoMDetailFacade().GetUoMDetailByID(uomMinID);
                    objKpiAssign.UoMEnableFlag = getKPI.UoMEnableFlag;
                    objKpiAssign.Verifier = CurrUser;
                    objKpiAssign.Weight = weight;
                    objKpiAssign.WeightEnableFlag = getKPI.WeightEnableFlag;
                    objKpiAssign.CreateBy = CurrUser.Email;


                    objKpiAssignDetail = new KPIAssignDetail();
                    objKpiAssignDetail.ID = 0;
                    objKpiAssignDetail.CreateBy = CurrUser.Email;
                    objKpiAssignDetail.Employee = CurrUser;
                    objKpiAssignDetail.isActive = true;
                    objKpiAssignDetail.KPI = getKPI;
                    objKpiAssignDetail.KPIAssign = objKpiAssign;
                    objKpiAssignDetail.ManualCalculation = getKPI.ManualCalculation;
                    objKpiAssignDetail.Polarization = getKPI.Polarization;
                    objKpi.UoM = new UoMFacade().GetUoMByID(uomID);
                    objKpi.UoMDetailMax = new UoMDetailFacade().GetUoMDetailByID(uomMaxID);
                    objKpi.UoMDetailMin = new UoMDetailFacade().GetUoMDetailByID(uomMinID);
                    objKpiAssignDetail.UoMEnableFlag = getKPI.UoMEnableFlag;
                    objKpiAssignDetail.Weight = weight;
                    objKpiAssignDetail.WeightEnableFlag = getKPI.WeightEnableFlag;
                    objKpiAssignDetail.Mandatory = true;
                    objKpiAssignDetail.AssignBy = CurrUser.ID;

                    if (KpiFacade.SaveKPIBreakDown(objKpi, "User", objKpiAssign, objKpiAssignDetail))
                        msg = "Transaction Success";
                    else
                        msg = "Transaction Error";


                    //KPIAssignDetail kad = new KPIAssignDetail()
                    //{
                    //    ID = 0,
                    //    CreateBy = CurrUser.Email,
                    //    Employee = CurrUser,
                    //    isActive = true,
                    //    KPI = KPIs,
                    //    KPIAssign = ka,
                    //    ManualCalculation = KPIs.ManualCalculation,
                    //    Polarization = KPIs.Polarization,
                    //    UoM = KPIs.UoM,
                    //    UoMDetailMax = KPIs.UoMDetailMax,
                    //    UoMDetailMin = KPIs.UoMDetailMin,
                    //    UoMEnableFlag = KPIs.UoMEnableFlag,
                    //    Weight = KPIs.Weight,
                    //    WeightEnableFlag = KPIs.WeightEnableFlag,
                    //    Mandatory = true,// ddlMandatory.SelectedValue == "1" ? true : false,
                    //    AssignBy = CurrUser.ID

                    //};

                    
                    //()
                    //{
                    //    ID = 0,
                    //    isActive = true,
                    //    KPI = KPIs,
                    //    Location = CurrUser.SubBranch.Location,
                    //    ManualCalculation = KPIs.ManualCalculation,
                    //    Polarization = KPIs.Polarization,
                    //    RGM = CurrUser.SubBranch.Location.RGM,
                    //    SubBranch = CurrUser.SubBranch,
                    //    UoM = KPIs.UoM,
                    //    UoMDetailMax = KPIs.UoMDetailMax,
                    //    UoMDetailMin = KPIs.UoMDetailMin,
                    //    UoMEnableFlag = KPIs.UoMEnableFlag,
                    //    Verifier = CurrUser,
                    //    Weight = KPIs.Weight,
                    //    WeightEnableFlag = KPIs.WeightEnableFlag,
                    //    CreateBy = CurrUser.Email
                    //};
                }
               
                
                
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                objSysLog.Description = ex.Message;
                objSysLog.LogEvent = "Input BreakDown KPI";
                objSysLog.Source = "KPIBreakDownHandler.ashx";
                objSysLog.CreatedBy = CurrUser.Email;
                objSysLog.CreatedDate = DateTime.Now;
                sysLogFacade.Save(objSysLog);
            }

            return msg;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}